var maxSubArray = function(nums) {
  const len = nums.length;
  let max = nums[0];

  // dp[i]表示以i结尾的子数组的最大和
  const dp = new Array(len);
  for (let i = 0; i < len; i++) {
    dp[i] = nums[i];
  }
  for (let i = 1; i < len; i++) {
    dp[i] = Math.max(dp[i], dp[i - 1] + nums[i]);
    max = Math.max(max, dp[i]);
  }
  return max;

};

const nums = [-2,1,-3,4,-1,2,1,-5,4];
console.log(maxSubArray(nums)); // Output: 6